//
//  HZL_StateViewController.m
//  B_Project
//
//  Created by 刘峻豪 on 15/11/28.
//  Copyright © 2015年 HZL. All rights reserved.
//

#import "HZL_StateViewController.h"
#import "HZL_NetworkingHandle.h"
#import "HZL_StoreModel.h"
#import "HZL_StateTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "HZL_SizeModel.h"
#import "HZL_StoreDetailViewController.h"

@interface HZL_StateViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UITableView *myTableView;
@property (nonatomic, retain)NSMutableArray *dataArr;
@property (nonatomic, retain) NSMutableDictionary *dataDic;
@property (nonatomic, strong) UIImageView *headerImage;
@property (nonatomic, strong) UILabel *detailLabel;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *subTitleLabel;
@property (nonatomic, strong) NSMutableDictionary *allData;
@end

@implementation HZL_StateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-xiangzuojiantou@3x"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];

//    self.automaticallyAdjustsScrollViewInsets = NO;
    self.dataArr = [NSMutableArray array];
    self.dataDic = [NSMutableDictionary dictionary];
    self.allData = [NSMutableDictionary dictionary];
    [self createTableView];
    [self getData];
}
//- (void)back
//{
//    self.tabBarController.tabBar.hidden = NO;
//    [self.navigationController popViewControllerAnimated:YES];
//}

#pragma mark - 创建tableView
- (void)createTableView
{
    self.myTableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.tableFooterView = [UIView new];
    [self.view addSubview:self.myTableView];
    [self.myTableView registerClass:[HZL_StateTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self createHeaderView];
}
- (void)createHeaderView
{
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 140)];
    self.myTableView.tableHeaderView = headerView;
    self.headerImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 140)];
    self.headerImage.contentMode = UIViewContentModeScaleAspectFill;
    self.headerImage.clipsToBounds = YES;
    [headerView addSubview:self.headerImage];
    // 背景高斯模糊
    UIBlurEffect *b = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *bV = [[UIVisualEffectView alloc] initWithEffect:b];
    bV.frame = self.view.frame;
    bV.alpha = 0.5;
    [self.headerImage addSubview:bV];
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.headerImage.frame.size.height / 2 - 30, VIEW_WIDTH, 30)];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.font = [UIFont boldSystemFontOfSize:22];
    self.titleLabel.textColor = [UIColor whiteColor];
    [headerView addSubview:self.titleLabel];
    
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(50, self.headerImage.frame.size.height / 2 +5, VIEW_WIDTH-100, 2)];
    line.backgroundColor = [UIColor whiteColor];
    [headerView addSubview:line];
    
    self.subTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.headerImage.frame.size.height/2 +10, VIEW_WIDTH, 25)];
    self.subTitleLabel.textAlignment = NSTextAlignmentCenter;
    self.subTitleLabel.textColor = [UIColor whiteColor];
    [headerView addSubview:self.subTitleLabel];
    
}
#pragma mark - 获取数据
- (void)getData
{
    NSString *str = @"http://www.molyo.com/mSubject/getInfoV1_7";
    NSString *body = [NSString stringWithFormat:@"device=iPhone7%%2C2&id=%@&netWork=wifi&os=ios%%209.100000", self.model.subId];
    [HZL_NetworkingHandle requestPOSTWithURl:str body:body completion:^(id result) {
        NSArray *tempArr = result[@"body"][@"contents"];
        self.dataArr = [HZL_StoreModel transformWithArray:tempArr];
        
        [self.headerImage sd_setImageWithURL:[NSURL URLWithString:result[@"body"][@"image"]]];
        self.titleLabel.text = result[@"body"][@"title"];
        self.subTitleLabel.text = result[@"body"][@"subTitle"];

        [self.myTableView reloadData];
    }];
}
#pragma mark - tableview协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}
//重用cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HZL_StateTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.model = self.dataArr[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
}

#pragma mark - 点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    HZL_StoreDetailViewController *sdVC = [[HZL_StoreDetailViewController alloc] init];
    sdVC.model = self.dataArr[indexPath.row];
//    sdVC.iD = [self.dataArr[indexPath.row] businessId];
    sdVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:sdVC animated:YES];
}
//- (void)viewWillDisappear:(BOOL)animated
//{
//    self.tabBarController.tabBar.hidden = YES;
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
